// 格式化日期
export const format = (date) => {
  if (date.length != 14) {
    return date
  }
  const year = date.substring(0, 4)
  const month = date.substring(4, 6)
  const day = date.substring(6, 8)
  const h = date.substring(8, 10)
  const m = date.substring(10, 12)
  const s = date.substring(12, 14)
  return `${year}-${month}-${day}  ${h}:${m}:${s}`
}

// 分位转换
export const milliFormat = (num) => {
  return num && num.toString().replace(/\d+/, function(s){
    return s.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
  })
}

// 格式化日期
export const formatDate = (formatType, formatDate) => {
  let type = formatType || "yyyy-mm-dd hh:mm:ss";
  let date = Object.prototype.toString.call(formatDate) === "[object Date]" ? formatDate : new Date();
  let currentdate = "";
  let month = date.getMonth() + 1;
  let strDate = date.getDate();
  let minutes = date.getMinutes();
  let seconds = date.getSeconds();
  month >= 1 && month <= 9 ? (month = "0" + month) : "";
  strDate >= 0 && strDate <= 9 ? (strDate = "0" + strDate) : "";
  minutes >= 0 && minutes <= 9 ? (minutes = "0" + minutes) : "";
  seconds >= 0 && seconds <= 9 ? (seconds = "0" + seconds) : "";

  switch (type.toLowerCase()) {
    case "yyyy-mm-dd hh:mm:ss":
      currentdate = date.getFullYear() + "-" + month + "-" + strDate + " " + date.getHours() + ":" + minutes + ":" + seconds;
      break;
    case "yyyy-mm-dd":
      currentdate = date.getFullYear() + "-" + month + "-" + strDate;
      break;
    case "yyyy-mm-dd hh:mm":
      currentdate = date.getFullYear() + "-" + month + "-" + strDate + " " + date.getHours() + ":" + minutes;
      break;
    case "yyyy-mm-dd hh":
      currentdate = date.getFullYear() + "-" + month + "-" + strDate + " " + date.getHours();
      break;
    case "yyyy-mm":
      currentdate = date.getFullYear() + "-" + month;
      break;
    case "yyyy":
      currentdate = date.getFullYear();
      break;
    default:
      currentdate = date.getFullYear() + "-" + month + "-" + strDate + " " + date.getHours() + ":" + minutes + ":" + seconds;
      break;
  }
  return currentdate;
}

// 当前日期前n天
export const getDay = (num, str) => {
  var today = new Date();
  var nowTime = today.getTime();
  var ms = 24*3600*1000*num;
  today.setTime(parseInt(nowTime + ms));
  var oYear = today.getFullYear();
  var oMoth = (today.getMonth() + 1).toString();
  if (oMoth.length <= 1) oMoth = '0' + oMoth;
  var oDay = today.getDate().toString();
  if (oDay.length <= 1) oDay = '0' + oDay;
  return oYear + str + oMoth + str + oDay;
}

export const formatFontSize = (size) => {
  let docEl = document.documentElement,
  clientWidth = window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth;
  if (!clientWidth) return;
  let fontSize = 100 * (clientWidth / 1920);
  return size * fontSize
}

// 获取取当前日期的上个月
export const getLastMonth = () =>{
  var date = new Date()
  var year = date.getFullYear()
  var month = date.getMonth()
  if(month == 0){
    year = year -1;
    month = 12; 
  }
  if (month < 10) {
    month = "0" + month
  }
  return year+"-"+month
}

export const getLastMonthCN = () =>{
  var date = new Date()
  var year = date.getFullYear()
  var month = date.getMonth()
  if(month == 0){
    year = year -1;
    month = 12; 
  }
  if (month < 10) {
    month = "0" + month
  }
  return year+ '年' + month +'月'
}

// 获取取当前日期的上n个月
export const getPreMonth = (n) =>{
  let now = new Date()
  return new Date(now.getFullYear(),now.getMonth()-n,now.getDate())
}

// 获取取当前月的第一天
export const getFirstDay = () =>{
  let date = new Date()
  return new Date(date.getFullYear(), date.getMonth(), 1)
}

export function formatTime(date, fmt) {
  if (date && date.toString().length === 10) {
    date = date * 1000
  }
  date = new Date(date)
  if (date === 'Invalid Date') {
    return ''
  }
  var o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'S': date.getMilliseconds()
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  }
  fmt = fmt.replace(/-/g, '.')
  return fmt
}

// 获取当前日期前n天
export function getBeforeDate(n){
	let date = new Date()
	let year,month,day,s
	date.setDate(date.getDate()-n)
	year = date.getFullYear()
	month = date.getMonth()+1
	day = date.getDate()
	s = year + '-' + ( month < 10 ? ( '0' + month ) : month ) + '-' + ( day < 10 ? ( '0' + day ) : day)
	return s
} 
